home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Multiplatform Code⁄Data Sharing / HelloBothWorlds / Libraries / stream.cpp < prev    next >
Encoding:
Text File  |  1997-06-26  |  1.2 KB  |  68 lines  |  [TEXT/CWIE]

  1.  
  2. // mail <chelly@eden.com> or surf http://www.eden.com/~chelly for feedback
  3. // free source code - do whatever you like with it
  4.  
  5. // sequential access to file data
  6. // automatically swaps multi-byte integral types, so it's easier to use
  7.  
  8. #include "stream.h"
  9. #include "byteorder.h"
  10.  
  11. // ** These are the special two functions
  12.  
  13. // read 2 bytes, then swap if required
  14. uint16 stream::get_mac_uint16()
  15. {
  16.     uint16 val;
  17.     get_bytes( &val, 2 );
  18.     SwapIfRequired( &val );
  19.     return val;
  20. }
  21.  
  22. // read 4 bytes, then swap if required
  23. uint32 stream::get_mac_uint32()
  24. {
  25.     uint32 val;
  26.     get_bytes( &val, 4 );
  27.     SwapIfRequired( &val );
  28.     return val;
  29. }
  30.  
  31. stream::stream( const char* path_of_file )
  32. {
  33.     m_file = fopen( path_of_file, "rb" );
  34.     assert( m_file );
  35. }
  36.  
  37. stream::~stream()
  38. {
  39.     int error = fclose( m_file );
  40.     assert( !error );
  41. }
  42.  
  43. uint8 stream::get_uint8()
  44. {
  45.     uint8 val;
  46.     get_bytes( &val, 1 );
  47.     return val;
  48. }
  49.  
  50. void    stream::set_mark( long new_mark )
  51. {
  52.     int error = fseek( m_file, new_mark, SEEK_SET );
  53.     assert( !error );
  54. }
  55.  
  56. void    stream::skip( long n_bytes )
  57. {
  58.     int error = fseek( m_file, n_bytes, SEEK_CUR );
  59.     assert( !error );    
  60. }
  61.  
  62. void    stream::get_bytes( void* storage, long n )
  63. {
  64.     long read = fread( storage, n, 1, m_file );
  65.     assert( n == 0 || read == 1 );
  66. }
  67.     
  68.